package su.product.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.product.dao.IProductDAO;
import su.product.entity.Product;


@Repository
public class ProductDAO extends SuBaseDAO implements IProductDAO {
	
	
	public List<Product> queryByPage(PagerDTO s) throws SQLException {
		String sql = "select * from su_product_product where status=1";
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql += " and id in ("+s.getS1()+")";
			}else{
				sql += " and id="+s.getS1();
			}
		}
		if(StringUtils.isNotBlank(s.getS2())){//jianpin
			sql += " and jianpin like '%"+s.getS2()+"%'";
		}
		if(StringUtils.isNotBlank(s.getS3())){//cateId
			sql += " and cateid="+s.getS3();
		}
		sql += " ORDER BY id desc";
		
		return super.queryByPage(sql, new RowMapper<Product>() {

			public Product mapRow(ResultSet rs, int arg1) throws SQLException {
				Product entity = new Product();
				entity.setId(rs.getInt("id"));
				entity.setCateid(rs.getInt("cateid"));
				entity.setPname(rs.getString("pname"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setUnit(rs.getString("unit"));
				entity.setStandard(rs.getString("standard"));
				entity.setPackstandard(rs.getDouble("packstandard"));
				entity.setPrice(rs.getDouble("price"));
				entity.setSafenum(rs.getDouble("safenum"));
				entity.setHighnum(rs.getDouble("highnum"));
				entity.setLownum(rs.getDouble("lownum"));
				entity.setPfunc(rs.getString("pfunc"));
				entity.setFreighttype(rs.getString("freighttype"));
				entity.setTotalnum(rs.getDouble("totalnum"));
				entity.setStatus(rs.getInt("status"));
				entity.setMinpayment(rs.getDouble("minpayment"));
				return entity;
			}
			
		}, s);
	}
	
	
	
	public Product getById(int id) throws SQLException {
		String sql = "select * from su_product_product where status=1 and id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<Product>() {
			public Product mapRow(ResultSet rs, int value) throws SQLException {
				Product entity = new Product();
				entity.setId(rs.getInt("id"));
				entity.setCateid(rs.getInt("cateid"));
				entity.setPname(rs.getString("pname"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setUnit(rs.getString("unit"));
				entity.setStandard(rs.getString("standard"));
				entity.setPackstandard(rs.getDouble("packstandard"));
				entity.setPrice(rs.getDouble("price"));
				entity.setSafenum(rs.getDouble("safenum"));
				entity.setHighnum(rs.getDouble("highnum"));
				entity.setLownum(rs.getDouble("lownum"));
				entity.setPfunc(rs.getString("pfunc"));
				entity.setFreighttype(rs.getString("freighttype"));
				entity.setTotalnum(rs.getDouble("totalnum"));
				entity.setStatus(rs.getInt("status"));
				entity.setMinpayment(rs.getDouble("minpayment"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "update su_product_product set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
	
	
	public void insert(Product o) throws SQLException {
		String sql = "insert into su_product_product(cateid ,pname ,jianpin ,unit ,standard ,packstandard ,price ,safenum ,highnum ,lownum ,pfunc ,freighttype ,totalnum ,status  ,minpayment ) values(? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? )";
		super.update(sql, new Object[]{o.getCateid() ,o.getPname() ,o.getJianpin() ,o.getUnit() ,o.getStandard() ,o.getPackstandard() ,o.getPrice() ,o.getSafenum() ,o.getHighnum() ,o.getLownum() ,o.getPfunc() ,o.getFreighttype() ,o.getTotalnum() ,o.getStatus() ,o.getMinpayment() });
	}
	
	
	
	public void update(Product o) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_product_product set ");
		List<Object> args = new ArrayList<Object>();
		args.add(o.getPname());
		updateSql.append("pname=?").append(",");
		args.add(o.getJianpin());
		updateSql.append("jianpin=?").append(",");
		args.add(o.getUnit());
		updateSql.append("unit=?").append(",");
		args.add(o.getStandard());
		updateSql.append("standard=?").append(",");
		args.add(o.getPackstandard());
		updateSql.append("packstandard=?").append(",");
		args.add(o.getPrice());
		updateSql.append("price=?").append(",");
		args.add(o.getSafenum());
		updateSql.append("safenum=?").append(",");
		args.add(o.getHighnum());
		updateSql.append("highnum=?").append(",");
		args.add(o.getLownum());
		updateSql.append("lownum=?").append(",");
		args.add(o.getPfunc());
		updateSql.append("pfunc=?").append(",");
		args.add(o.getFreighttype());
		updateSql.append("freighttype=?").append(",");
		args.add(o.getMinpayment());
		updateSql.append("minpayment=?").append(",");
			
		
		args.add(o.getId());
			
		
		String sql = updateSql.substring(0, updateSql.length()-1) + " where id = ? "; 
		super.update(sql, args.toArray());
	}

	public void updatePrice(int productId,double price) throws SQLException{
		String sql = "update su_product_product set price=? where id=?";
		super.update(sql, new Object[]{price,productId});
	}
	
	public void updateProductCate(int productId,int oldCateId,int newCateId)throws Exception{
		String sql = "update su_product_product set cateid=? where id=?";
		super.update(sql, new Object[]{newCateId,productId});
	}
	
	public void updateTotalNum(int productId,double totalNum)throws Exception{
		String sql = "update su_product_product set totalnum=? where id=?";
		super.update(sql, new Object[]{totalNum,productId});
	}
}

